# coding:utf-8

# 2.3.4 实战：淘宝商品比价定向爬虫
import re
import requests


# 获取页面html
def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
        'cookie': 'miid=1535482570367186800; cookie2=6e87dd11b48dc75f227cf036d0f6bbe4; v=0; _tb_token_=f30be8ee41773; cna=uVskEQsl1BUCAat42oaQHKSA; tk_trace=oTRxOWSBNwn9dPyorMJE%2FoPdY8zZPEr%2FCrvCMS%2BG3sTRRWrQ%2BVVTl09ME1KrXdbYQyWDzjYhOx%2BUhHxroKQnemVLj0tU1xd75TGkzn3M%2BOx%2BRxMZtWd9pBWDUD8hgWXixe51H1qGhS3lPHKNV5oHwqAoM4jWOpy3fFSzsKRMIK7SLYeVxYSkdRYiH0LpoDWhDU399Yy5RCyBbezDJsMU%2BDy5JkyvlE58OeXQk2CHXicUxVDORUhrJsHE7AIWHzsDJty%2FWAT43VBG8dqxzgHmQZtX1CAck0aRxzrEBNwgMmcBBYK4HfOPALv2qkU%2BV%2FsHWuRS1JTvQXDbJonfcv4Q; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; t=7d095be8dae051b617612fc7f71d2f73; _samesite_flag_=true; dnk=%5Cu5F20719923505; tracknick=%5Cu5F20719923505; tg=0; sgcookie=EBXkkWPDmBc28Ae7TLO1q; uc3=lg2=VT5L2FSpMGV7TQ%3D%3D&id2=UoH5Z3NI%2FVRAOA%3D%3D&vt3=F8dBxdGMQvj9crMoa0s%3D&nk2=ttSzTPETU5LGukk%3D; csg=b64efe69; lgc=%5Cu5F20719923505; skt=5f1d40fba83c080d; existShop=MTU4NjQzODEzMg%3D%3D; uc4=nk4=0%40tAIH2w1hONUpvO6BX3cAYkdYxnENJQ%3D%3D&id4=0%40UOnmPLs9OcRO0eJtal2%2BZ2sfX2x9; _cc_=URm48syIZQ%3D%3D; tfstk=cfMCBA44RwbIxDb8C2taTpqF3We5ZzX_jO4ZAfdMKBUPymoCixW4lpnLAN6TMl1..; enc=9BidDucCCWfSGNd7u1LfxaL%2BbxWtMcDLAWI2KvRlpnlRlLuJwIm%2FYWgJUmGrRZXl7bgb85k9ZWyrPW%2BxhGtHng%3D%3D; mt=ci=-1_0; uc1=cookie16=URm48syIJ1yk0MX2J7mAAEhTuw%3D%3D&cookie21=VFC%2FuZ9aiKCaj7AzMHh1&cookie15=UIHiLt3xD8xYTw%3D%3D&existShop=false&pas=0&cookie14=UoTUPcqYgnhT0Q%3D%3D&tag=8&lng=zh_CN; l=eBjd8dAmq3WMRSGkBO5whurza77OrQdfh1PzaNbMiIHca1uR1iDNVNQccDRvRdtjgt5veFtykJ0GkRE9SyULRKgKqelyRs5mpi9wRe1..; isg=BD09zAcBP_1k85iHhgNU689STJk32nEs9qHNn_-BuRRlNltowymW_JHk4GpwsYnk; JSESSIONID=E554CC8A495277CB6B6EB9F72C62F530'
    }
    try:
        r = requests.get(url, headers=headers)
        # print(res.text)
        # pattern = '<script>(.*?)g_page_config = (.*?)</script>'
        # r = re.compile(pattern, re.S).findall(res.text)
        # print(type(res[0]))
        # print(res[0])
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ''


# 解析页面数据
def parse_html(glist, html):
    try:
        # 使用正则表达式提取信息
        # 商品价格
        # price_list = re.findall(r'<div class="price g_price g_price-highlight">(.*?)<span>￥</span>(.*?)<strong>(\d?)</strong></div>', html)
        # # 商品名称
        # name_list = re.findall(r'<div class="row row-2 title"><a>(.*?)</a></div>', html)
        price_list = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html)
        name_list = re.findall(r'\"raw_title\"\:\".*?\"', html)
        print('name=', name_list)
        for i in range(len(price_list)):
            price = eval(price_list[i].split(":")[1])  # eval（）在此可以去掉""
            name = eval(name_list[i].split(":")[1])
            glist.append([price, name])
        return glist
    except:
        print("解析失败")
        return []


def print_goods_list(glist):
    tplt = "{0:^4}\t{1:^6}\t{2:^15}"
    print(tplt.format("序号", "商品价格", "商品名称"))
    count = 0
    for g in glist:
        count = count + 1
        print(tplt.format(count, g[0], g[1]))


q = '书包'
start_url = "https://s.taobao.com/search?q=" + q
info_list = []
page = 3

count = 0
for i in range(page):
    count += 1
    try:
        url = start_url + "&s=" + str(44 * i)
        html = get_html_text(url)  # 爬取url
        parse_html(info_list, html)  # 解析HTML和爬取内容
        print("\r爬取页面当前进度: {:.2f}%".format(count * 100 / page), end="")  # 显示进度条
    except:
        continue
print()
print_goods_list(info_list)
